/**
 * 计算参数的占用字节数
 */

function sizeOfObject(object) {
    if(!object) return null;
    let bytes = 0;

    const seen = new WeakMap()
    const porperties = Object.keys(object)
    for(let i=0; i<porperties.length; i++){
        const key = porperties[i]
        bytes += calculator(key)
        if(typeof object[key] === 'object' && object[key] !== null){
            if(seen.has(object[key])){
                continue;
            }
            seen.add(object[key])
        }
        bytes += calculator(object[key])
    }
    return bytes;
}

function calculator(object) {
    const objectType = typeof object;
    switch (objectType){
        case 'string':{
            return object.length * 2
        }
        case "number":{
            return 8
        }
        case "boolean":{
            return  4;
        }
        case "object":{
            if(Array.isArray(object)){
                return  object.map(calculator).reduce((res, cur) => res +cur, 0)
            }else {
                return  sizeOfObject(object)
            }
        }
    }
}

console.log(calculator(1));
console.log(calculator('12'));
console.log(calculator([1, 2]));
console.log(calculator([{name: 'ck'},{xxx:'ck'}]));
